package db

import (
	"log"
)

// Group
func (m *MysqlStruct) NewGroup(r *Group) error {
	//tx := m.Db.Begin()
	tx := m.Db
	if err := tx.Create(&r).Error;err!=nil{
		//tx.Rollback()
		return err
	}
	//tx.Commit()
	return nil
}
// 通过id更新Group信息
func (m *MysqlStruct) UpdateGroupById(id int,updates *Group) error {
	//db := m.Db.Begin()
	db := m.Db
	obj := Group{}
	if err :=db.Where("id = ?",id).First(&obj).Error ;err !=nil{
		return err
	}
	if err := db.Model(&obj).Updates(updates).Error;err != nil{
		//db.Rollback()
		return err
	}
	return db.Model(&obj).Association("Instances").Replace(updates.Instances)

}

// 通过groupid增加instance
func (m *MysqlStruct)UpdateGroupByIdAddAInstance(groupid int,i *[]Instance)error{
	db := m.Db
	obj := Group{}
	if err :=db.Where("id = ?",groupid).First(&obj).Error ;err !=nil{
		return err
	}
	err := db.Model(&obj).Association("Instances").Append(i)
	if err!=nil{
		log.Println(err,"------")
		return err
	}
	return nil
}

// 通过groupname查找组
func (m *MysqlStruct)GetGroupByName(groupName string,i *Group)error{
	db := m.Db
	return db.Where("group_name = ?",groupName).First(&i).Error
}

// 通过groupname增加instance
func (m *MysqlStruct)UpdateGroupByNameAddAInstance(groupName string,i *[]Instance)error{
	db := m.Db
	obj := Group{}
	if err :=db.Where("group_name = ?",groupName).First(&obj).Error ;err !=nil{
		return err
	}
	err := db.Model(&obj).Association("Instances").Append(i)
	if err!=nil{
		log.Println(err,"------")
		return err
	}
	return nil
}

// 通过服务查询任务状态对象
func (m *MysqlStruct)GetGroupByGroupId(groupid int,r *Group)  error {
	db := m.Db
	return db.Where("id = ?",groupid).First(&r).Error
}

// 通过服务查询任务状态对象
func (m *MysqlStruct)GetGroupByServers(pageSize,pageNum int,serverName string,r *[]Group)  error {
	tx := m.Db
	return tx.Preload("Instances").Where(&Group{ServerName: serverName}).Limit(pageSize).Offset((pageNum-1)*pageSize).Find(&r).Error
	// find xx .limit(xx)
}

// 通过服务查询任务状态对象
func (m *MysqlStruct)GetGroups(pageSize,pageNum int, r *[]Group)  error {
	tx := m.Db
	return tx.Preload("Instances").Limit(pageSize).Offset((pageNum-1)*pageSize).Find(&r).Error
	// find xx .limit(xx)
}

// 通过服务查询任务状态对象
func (m *MysqlStruct)GetAllGroups(r *[]Group)  error {
	tx := m.Db
	return tx.Preload("Instances").Find(&r).Error
	// find xx .limit(xx)
}

// 更新group的组中实例
func (m *MysqlStruct)UpdateGroupInstances(groupid int,i *[]Instance)error{
	db := m.Db
	obj := Group{}
	if err :=db.Where("id = ?",groupid).First(&obj).Error ;err !=nil{
		return err
	}
	err := db.Model(&obj).Association("Instances").Replace(i)
	if err!=nil{
		log.Println(err,"------")
		return err
	}
	return nil
}

// 查询一个组里所有的实例
func (m *MysqlStruct)GetInstanceByGroupId(groupid int,i *[]Instance)error{
	db := m.Db
	obj := Group{}
	if err :=db.Where("id = ?",groupid).First(&obj).Error ;err !=nil{
		return err
	}
	return db.Model(&obj).Association("Instances").Find(&i)
}

// 查询一个组里所有的实例
func (m *MysqlStruct)GetInstanceByGroup(group Group,i *[]Instance)error{
	db := m.Db
	return db.Model(&group).Association("Instances").Find(&i)
}

// 删除一个分组
func (m *MysqlStruct)DeleteGroupById(groupid int)error{
	db := m.Db
	obj := Group{}
	if err :=db.Where("id = ?",groupid).First(&obj).Error ;err !=nil{
		return err
	}
	return db.Delete(&obj).Error
}

// 通过主机名 来查询它所在的组名
func (m *MysqlStruct)GetGroupNameByInstanceName(instanceName string,GroupName *string)error{
	db := m.Db
	groupObj := Group{}
	instaceObj := Instance{}
	err := m.GetInstanceByHostname(instanceName,&instaceObj)
	if err!=nil{
		return err
	}
	//log.Println(instaceObj)

	if err = db.Model(&instaceObj).Association("Groups").Find(&groupObj); err!=nil{return err}
	//log.Println("group:",groupObj)

	*GroupName = groupObj.ServerName
	return nil
}

// 通过主机名 来查询它属于哪些服务
func (m *MysqlStruct)GetServerNameByInstanceName(instanceName string,groups *[]string)error{

	db := m.Db
	groupObj := []Group{}
	instaceObj := Instance{}
	err := m.GetInstanceByHostname(instanceName,&instaceObj)
	if err!=nil{
		return err
	}
	//log.Println(instaceObj)

	if err = db.Model(&instaceObj).Association("Groups").Find(&groupObj); err!=nil{return err}
	// log.Println("group:",groupObj)
	for _, group := range groupObj {
		*groups=append(*groups, group.ServerName)
	}

	return nil
}